﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Estsh.Core.Base;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Estsh.Core.Services;
using Estsh.Core.Repositories;
using Estsh.Core.Models;
using Estsh.Core.Model.Extend.Models;
using Newtonsoft.Json.Linq;
using static Estsh.Core.Model.EnumUtil.WmsEnumUtil;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Wms.Repositories;
using Estsh.Core.Model.Extend;
using Newtonsoft.Json;

namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// 移动单服务类
    /// </summary>
    public class MoveOrderService : BaseService<WmsMoveHeader>, IMoveOrderService
    {
        private readonly IMoveOrderRepository repository;
        private readonly IStockRepository stockRepository;

        public MoveOrderService(IMoveOrderRepository _repository,
            IStockRepository _stockRepository) : base(_repository)
        {
            this.repository = _repository;
            this.stockRepository = _stockRepository;
        }

        /// <summary>
        /// 获取正在创建中的单据
        /// </summary>
        /// <returns></returns>
        public List<WmsMoveHeader> GetCreateingList()
        {
            return repository.GetCreateingList();
        }

        /// <summary>
        /// 处理扫描的箱条码
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="orderNo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public WmsResponseResult_Extend DoCarton(string cartonNo, string orderNo, int orderType, string loginId, string relevanceOrder)
        {
            SysStock_Extend cartonInfo = stockRepository.GetCartonInfo(cartonNo);
            //WmsMoveSn moveSnInfo = repository.GetMovesnInfo(cartonNo);

            WmsResponseResult_Extend result = new WmsResponseResult_Extend();

            //if (moveSnInfo != null)
            //{
            //    result.Msg = "该条码已生成对应的单据信息！";
            //    return result;
            //}
            if (cartonInfo == null)
            {
                result.Msg = "包装条码不存在，请检查！";
                return result;
            }
            if (cartonInfo.Enabled.Equals(Enabled.N))
            {
                result.Msg = "包装条码已被冻结，不允许此操作！";
                return result;
            }
            ////NC让步，
            //if (orderType == (int)MoveOrderType.NC_CONCESSION)
            //{
            //    if (cartonInfo.Status != (int)StockStatus.NC_INSTOCK || cartonInfo.Status != (int)StockStatus.NC_QUARANTINE)
            //    {
            //        result.Msg = "包装条码状态不是NC入库状态，不允许此操作！";
            //        return result;
            //    }
            //}
            //NC报废 NC让步，NC入库、NC隔离状态
            if ( orderType == (int)MoveOrderType.NC_SCRAP || orderType == (int)MoveOrderType.NC_CONCESSION)
            {
                if (cartonInfo.Status != (int)StockStatus.NC_INSTOCK  && cartonInfo.Status != (int)StockStatus.NC_QUARANTINE)
                {
                    result.Msg = "包装条码状态不是NC入库状态或NC隔离状态，不允许此操作！";
                    return result;
                }               
            }
            //NC入库，除已报废、已出库状态
            if (orderType == (int)MoveOrderType.NC_IN_STOCK)
            {
                if (cartonInfo.Status == (int)StockStatus.SCRAPED || cartonInfo.Status == (int)StockStatus.OUTSTOCKED)
                {
                    result.Msg = "包装条码状态错误，不允许此操作！";
                    return result;
                }
            }



            //if (cartonInfo.Status != (int)StockStatus.INSTOCKED 
            //    && orderType == (int)MoveOrderType.NC_IN_STOCK)
            //{
            //    result.Msg = "包装条码状态不是在库状态，不允许此操作！";
            //    return result;
            //}


            WmsMoveDetail moveDetail = null;
            if (!string.IsNullOrEmpty(orderNo))
            {
                moveDetail = repository.GetMoveDetail(orderNo, cartonInfo.PartNo);
                if (moveDetail != null && cartonInfo.ZoneId != moveDetail.SrcZoneId)
                {
                    result.Msg = "包装条码对应的存储区与单据中此零件的源存储区不一致！";
                    return result;
                }

                WmsMoveSn moveSn = repository.GetMoveSn(orderNo, cartonInfo.CartonNo);

                if (moveSn != null)
                {
                    result.Msg = "单据中包含此包装条码，不允许此操作！";
                    return result;
                }

                if (cartonInfo.RelevanceOrder != relevanceOrder)
                {
                    result.Msg = "请扫描关联单号一致的箱条码！";
                    return result;
                }
            }

            string resOrderNo = repository.DoCarton(cartonInfo, orderNo, orderType, loginId, moveDetail);

            if (!string.IsNullOrEmpty(resOrderNo))
            {
                //result = this.SubmitOrder(orderNo, loginId);
                result.Msg = "SCAN_OK";
                result.Data = resOrderNo;
                result.ResRelevanceOrder =cartonInfo.RelevanceOrder;
            }
            else
            {
                result.Msg = "操作失败，请重新尝试！";
            }

            return result;
        }

        /// <summary>
        /// 提交单据
        /// </summary>
        /// <param name="orderNo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public WmsResponseResult SubmitOrder(string orderNo, string loginId)
        {
            WmsResponseResult result = new WmsResponseResult();
            // 完成单据并提交接口
            if (repository.OrderCreated(orderNo, loginId))
            {
                result.Msg = "SCAN_OK";
                result.Data = orderNo;
            }
            else
            {
                result.Msg = "操作失败，请重新尝试！";
            }
            return result;
        }



        /// <summary>
        /// 根据单据类型获取此类型待处理的单据列表,如果单据类型为空，则获取全部类型
        /// </summary>
        /// <returns></returns>
        public List<WmsMoveHeader> GetMoveOrderList(int? orderType,string orderNo)
        { 
           return repository.GetMoveOrderList(orderType,orderNo);
        }

        /// <summary>
        /// 根据单据号获取单据明细列表
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public List<WmsMoveDetail> GetMoveDetailList(string orderNo)
        {
            return repository.GetMoveDetailList(orderNo);
        }
    }
}
